package com.wolfalpha.lwtool.service.impl;

import org.apache.http.HttpResponse;
import org.apache.http.message.BasicNameValuePair;
import com.wolfalpha.lwtool.constant.Application;
import com.wolfalpha.lwtool.service.SaveService;

public class SaveServiceImpl implements SaveService {

	// 接口地址
	private static final String serviceURL = "http://2.jianzhibaobei.sinaapp.com/Api/Job/publish";
	private static final String serviceURL1 = "http://2.jianzhibaobei.sinaapp.com/Api/User/login";
	/**
	 * 存储数据前 用户登录
	 */
	public int login(String user, String password) throws Exception {
		HttpResponse message = null;
		try {
			message = Application.getClient().nonIdmpRequest(
					serviceURL1, new BasicNameValuePair("user", user),
					new BasicNameValuePair("password", password));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int status = message.getStatusLine().getStatusCode();
		System.out.println(status);
		if (status == 200) {
			System.out.println("Login Success");
			return 1;
		}
		
		return 0;
	}

	@Override
	public int publishJob(String name, int category, String contact,
			String tel, int region, String address, int wage, int wageType,
			int time, String days, int limit, String intro, String requirement)
			throws Exception {
		HttpResponse message = null;
		try {
			message = Application
					.getClient()
					.unSafeIdmpRequest(
							serviceURL,
							new BasicNameValuePair("name", name),
							new BasicNameValuePair("category", String
									.valueOf(category)),
							new BasicNameValuePair("contact", contact),
							new BasicNameValuePair("tel", tel),
							new BasicNameValuePair("region", String
									.valueOf(region)),
							new BasicNameValuePair("address", address),
							new BasicNameValuePair("wage", String.valueOf(wage)),
							new BasicNameValuePair("wage_type", String
									.valueOf(wageType)),
							new BasicNameValuePair("time", String.valueOf(time)),
							new BasicNameValuePair("days", days),
							new BasicNameValuePair("limit", String
									.valueOf(limit)),
							new BasicNameValuePair("intro", intro),
							new BasicNameValuePair("requirement", requirement));
		} catch (Exception e) {
			e.printStackTrace();
		}

		int status = message.getStatusLine().getStatusCode();
		if (status == 200) {
			return 1;// 成功
		} else {
			return 0;
		}
	}

}
